﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

namespace RenRenClient
{
    public delegate void GetFeedCallback(List<RennFeed> ar);

    public class RennFeedMedia
    {
        /// <summary>
        /// 媒体内容的链接地址
        /// </summary>
        public string Href { get; set; }

        /// <summary>
        /// 媒体内容的类型，目前有“photo”, “album”, “link”, “video”, “audio”, “status”
        /// </summary>
        public string MediaType { get; set; }

        /// <summary>
        /// 媒体内容的原地址
        /// </summary>
        public string Src { get; set; }

        /// <summary>
        /// 媒体内容的id，例如相片的id
        /// </summary>
        public string MediaID { get; set; }

        /// <summary>
        /// 媒体内容的所有者id feed_media子节点 
        /// </summary>
        public string OwnerID { get; set; }
    }

    public class RennComment
    {
        /// <summary>
        /// 发表评论用户的id
        /// </summary>
        public string UID { get; set; }

        /// <summary>
        /// 发表评论的用户姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 发表评论的用户头像
        /// </summary>
        public string HeadURL { get; set; }

        /// <summary>
        /// 评论的时间
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// 评论的id
        /// </summary>
        public string CommentID { get; set; }

        /// <summary>
        /// 评论的内容
        /// </summary>
        public string Text { get; set; }
    }


    public class RennFeed
    {
        /// <summary>
        /// 新鲜事的id
        /// </summary>
        public string PostID { get; set; }

        /// <summary>
        /// 新鲜事内容主体的id，例如日志id、相册id和分享id等等
        /// </summary>
        public string SourceID { get; set; }

        /// <summary>
        /// 新鲜事类型
        /// </summary>
        public string FeedType { get; set; }

        /// <summary>
        /// 新鲜事更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }

        /// <summary>
        /// 新鲜事用户的id
        /// </summary>
        public string ActorID { get; set; }

        /// <summary>
        /// 新鲜事用户的姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 新鲜事用户的头像
        /// </summary>
        public string HeadUrl { get; set; }

        /// <summary>
        /// 新鲜事内容的前缀
        /// </summary>
        public string Prefix { get; set; }

        /// <summary>
        /// 新鲜事用户自定义输入内容，状态
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 新鲜事的主题内容
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 新鲜事主题的相关链接
        /// </summary>
        public string Href { get; set; }

        /// <summary>
        /// 新鲜事的具体内容
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 新鲜事中包含的媒体内容，例如照片、视频等 
        /// </summary>
        public RennFeedMedia[] Attachments { get; set; }

        /// <summary>
        /// 评论的数量
        /// </summary>
        public int CommentsCount { get; set; }

        /// <summary>
        /// 新鲜事中包含的评论内容，目前返回最新和最早的评论 
        /// </summary>
        public RennComment[] Comments { get; set; }

        public static List<RennFeed> Parse(XDocument data)
        {
            XName qualifiedName = XName.Get("feed_post", RennURL.XmlNS);
            var xfeed = from item in data.Descendants(qualifiedName)
                        select item;
            List<RennFeed> rtn = new List<RennFeed>();
            foreach (var item in xfeed)
            {
                RennFeed feed = new RennFeed();

                //ID = item.Element(XName.Get("id", RennURL.XmlNS)).Value,
                feed.PostID = item.Element(XName.Get("post_id", RennURL.XmlNS)).Value;
                feed.SourceID = item.Element(XName.Get("source_id", RennURL.XmlNS)).Value;
                feed.FeedType = item.Element(XName.Get("feed_type", RennURL.XmlNS)).Value;
                feed.UpdateTime = DateTime.Parse(item.Element(XName.Get("update_time", RennURL.XmlNS)).Value);
                feed.ActorID = item.Element(XName.Get("actor_id", RennURL.XmlNS)).Value;
                feed.Name = item.Element(XName.Get("name", RennURL.XmlNS)).Value;
                feed.HeadUrl = item.Element(XName.Get("headurl", RennURL.XmlNS)).Value;
                //feed.Prefix = item.Element(XName.Get("prefix", RennURL.XmlNS)).Value;
                switch (feed.FeedType)
                {
                    case "10":
                        feed.Description = feed.Message = item.Element(XName.Get("message", RennURL.XmlNS)).Value;
                        break;
                    case "50":
                    case "51":
                    case "52":
                        feed.Description = feed.Title = item.Element(XName.Get("title", RennURL.XmlNS)).Value;
                        break;
                    default:
                        feed.Description = item.Element(XName.Get("description", RennURL.XmlNS)).Value;
                        break;
                }

                //feed.Title = item.Element(XName.Get("title", RennURL.XmlNS)).Value;
                //feed.Href = item.Element(XName.Get("href", RennURL.XmlNS)).Value;
                

                rtn.Add(feed);
            }
            return rtn;
        }

    }
}


